﻿using ConsoleApp_EXL;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET
{
    public static class GenerateDbBlock
    {   
        public static void GeberateDbBlockFromTable(List<InputTableRow> rows, string ?path)
        {
            using (FileStream exlFile = new FileStream(path + @"/数据块_2.db", FileMode.Create, FileAccess.ReadWrite)) //创建文件
            {
                StreamWriter writer = new StreamWriter(exlFile, Encoding.UTF8);
                //提取变量分类
                var InPut_1 = rows.Where(row => row.InOut == "In" & row.Name != @"/").GroupBy(comparer => comparer.Group);      //输入变量只有一级分组
                var OutPut_1 = rows.Where(row => row.InOut == "Out" & row?.Name != @"/").GroupBy(comparer => comparer.Group);    //输出变量只有一级分组

                        
                
                
                #region 只有一级分组
                //写入块头                                                                  
                writer.WriteLine("\tDATA_BLOCK \"数据块_2\"");    //数据块名称
                writer.WriteLine("{ S7_Optimized_Access := 'TRUE' }");
                writer.WriteLine("VERSION : 0.1");
                writer.WriteLine("NON_RETAIN");

                //写入变量
                writer.WriteLine("   STRUCT ");
                writer.WriteLine("      Input : Struct");

                //输入变量写入
                foreach (var row in InPut_1)
                {
                    var InPut_2 = row.Where(row => row.InOut == "In" & row.Name != @"/").GroupBy(comparer => comparer.Group2); //输入变量带有二级分组

                    if (InPut_2.Count() <= 1)
                    {
                        WriteVariables(row, writer, 1);
                    }

                    if ( InPut_2.Count() > 1)
                    {

                        for (int i = 0; i < InPut_2.Count(); i++)
                        {
                            if (i == 0)
                            {
                                writer.WriteLine("      " + InPut_2.ToList()[i].ToList()[0].Group + " : Struct");
                            }
                            if (InPut_2.ToList()[i].Key == @"/")
                            {
                                WriteVariables(InPut_2.ToList()[i], writer, 3);
                            }
                            else
                            {
                                WriteVariables(InPut_2.ToList()[i], writer, 2);
                            }
                            if (i == InPut_2.Count() - 1)
                            {
                                writer.WriteLine("      END_STRUCT;");
                            }
                        }
                    }
                }

                writer.WriteLine("      END_STRUCT;");

                writer.WriteLine("     Output : Struct");

                //输出变量写入
                foreach (var row in OutPut_1)
                {
                    var OutPut_2 = row.Where(row => row.InOut == "Out" & row?.Name != @"/").GroupBy(comparer => comparer.Group2);     //输出变量带有二级分组

                    if (OutPut_2.Count() <= 1)
                    {
                        WriteVariables(row, writer, 1);
                    }

                    if (OutPut_2.Count() > 1)
                    {
                        for (int i = 0; i < OutPut_2.Count(); i++)
                        {
                            if (i==0)
                            {
                                writer.WriteLine("      " + OutPut_2.ToList()[i].ToList()[0].Group + " : Struct");
                            }
                            if (OutPut_2.ToList()[i].Key == @"/")
                            {
                                WriteVariables(OutPut_2.ToList()[i], writer, 3);
                            }
                            else
                            {
                                WriteVariables(OutPut_2.ToList()[i], writer, 2);
                            }
                            if (i == OutPut_2.Count()-1)
                            {
                                writer.WriteLine("      END_STRUCT;");
                            }
                        }                      
                    }
                }
                
                writer.WriteLine("      END_STRUCT;");
                writer.WriteLine("   END_STRUCT;");
                writer.WriteLine();
                writer.WriteLine();
                writer.WriteLine("BEGIN");
                writer.WriteLine();
                writer.WriteLine("END_DATA_BLOCK"); 
                #endregion

                //关闭文件
                writer.Close();
            } ;

            

        }

        static void WriteVariables(IEnumerable<InputTableRow> rows, StreamWriter writer, int GroupNumber) 
        {
            string ?groupName = "";
            if (GroupNumber == 1)
            {
                groupName = rows.ToList()[0].Group;
            }
            else if (GroupNumber == 2)
            {
                groupName = rows.ToList()[0].Group2;
            }

            if (GroupNumber == 1 || GroupNumber == 2)
            {
                writer.WriteLine("      " + groupName + " : Struct");

                foreach (var item in rows)
                {
                    writer.WriteLine("          " + item.Name + " : " + item.Type + ";  //" + item.Notes);
                }

                writer.WriteLine("      END_STRUCT;"); 
            }

            if (GroupNumber == 3)
            {
                foreach (var item in rows)
                {
                    writer.WriteLine("          " + item.Name + " : " + item.Type + ";  //" + item.Notes);
                }
            }
        }

        
    }
}
